﻿using NewToDoList.logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToDoList.Entity.Models;

namespace ToDoList
{
    public class Program
    {
        private static todoContext _ctx = new todoContext();
        OperateMatter operateMatter = new OperateMatter();
        OperateList operateList = new OperateList();
        public void Menu()
        {
          Console.WriteLine("1.Creat List\n" +
                            "2.Add Matter\n" +
                            "3.Finish Matter\n" +
                            "4.Look all of Matter\n" +
                            "5.Look Unfinish Matter\n" +
                            "6.Look All of List \n" +
                            "7.Look overdue Matter\n" +
                            "8.Delete Matter\n" +
                            "9.Cancel Finish Matter\n" +
                            "10.exit\n");
        }

        public void CreatListInMenu(int id)
        {

            Console.Write("please input the name of list:");
            string nameOfList = Console.ReadLine();
            if (operateList.CreatDetailedList(nameOfList, id))
                Console.WriteLine("Add List Success!");
            else
                Console.WriteLine("Add List Failed!");
            CommonPrint();
        }

        public void AddMatterInMenu()
        {
            Console.WriteLine("please input you content:");
            string content = Console.ReadLine();
            Console.WriteLine("please input the past time:");
            string pastTime = Console.ReadLine();

            Console.WriteLine("Please choose the list Id you want to add:");
            string inputNumberOfList = Console.ReadLine();
            int listId = Int32.Parse(inputNumberOfList);
            if (operateMatter.AddMatter(listId, pastTime, content))
                Console.WriteLine("Add Matter Success!");
            else
                Console.WriteLine("Add Matter Failed!");
            CommonPrint();
        }


        public void FinishMatterInMenu(int id)
        {
            List<string> matter = printMatter(_ctx.Matter.ToList());
            foreach (var item in matter)
                Console.WriteLine(item);
            Console.WriteLine("Please choose the Matter Id you want to Finish:");
            string inputNumberOfList = Console.ReadLine();
            int matterId = Int32.Parse(inputNumberOfList);

            if (operateMatter.FinishMatter(matterId, id))
                Console.WriteLine("Finish Matter Success!");
            else
                Console.WriteLine("Finish Matter Failed");
            CommonPrint();
        }

        public void ViewAllMatterInMenu(int id)
        {
            List<string> matter = printMatter(operateMatter.ViewAllMatter(id));
            if (operateMatter.ViewAllMatter(id) != null)
                foreach (var item in matter)
                    Console.WriteLine(item);
            CommonPrint();
        }

        public void ViewAllUnFinishInMenu(int id)
        {
            List<string> matter = printMatter(operateMatter.ViewUnfinishMatter(id));
            if (operateMatter.ViewUnfinishMatter(id) != null)
                foreach (var item in matter)
                    Console.WriteLine(item);
            CommonPrint();
        }

        public void ViewAllListInMenu(int id)
        {
            List<string> list = PrintDetailedListInfro(operateList.ViewAllDetailedList(id));
            if (operateList.ViewAllDetailedList(id) != null)
                foreach (var item in list)
                    Console.WriteLine(item);
            CommonPrint();
        }

        public void ViewOverDueMatterInMenu()
        {
            List<string> matter = printMatter(operateMatter.OverdueMatterChangeStatus());
            foreach (var item in matter)
                Console.WriteLine(item);
            CommonPrint();
        }

        public void DeleteMatterInMenu(int id)
        {
            List<string> matter = printMatter(_ctx.Matter.ToList());
            foreach (var item in matter)
                Console.WriteLine(item);
            Console.WriteLine("Please choose the Matter Id you want to delete:");
            string inputNumberOfList = Console.ReadLine();
            int matterId = Int32.Parse(inputNumberOfList);

            if (operateMatter.DeleteMatter(matterId, id))
                Console.WriteLine("Cancel Finish Matter Success!");
            else
                Console.WriteLine("Cancel Finish Matter Failed");
            CommonPrint();
        }

        public void CancelFinishMatterInMenu(int id)
        {
            List<string> matter = printMatter(_ctx.Matter.ToList());
            foreach (var item in matter)
                Console.WriteLine(item);
            Console.WriteLine("Please choose the Matter Id you want to cancel Finish:");
            string inputNumberOfList = Console.ReadLine();
            int matterId = Int32.Parse(inputNumberOfList);

            if (operateMatter.CancelFinishMatter(matterId))
                Console.WriteLine("Cancel Finish Matter Success!");             
            else
                Console.WriteLine("Cancel Finish Matter Failed");
            CommonPrint();

        }

        public void LoginSystem(int id)
        {
            while (true)
            {

                Menu();
                Console.Write("please input you choice: ");
                string inputNumber = Console.ReadLine();
                int choice = Int32.Parse(inputNumber);
                switch (choice)
                {
                    case 1:CreatListInMenu(id);break;                                            
                    case 2:AddMatterInMenu();break;                           
                    case 3:FinishMatterInMenu(id);break;
                    case 4: ViewAllMatterInMenu(id);break;                       
                    case 5:ViewAllUnFinishInMenu(id); break;                       
                    case 6:ViewAllListInMenu(id);break;                        
                    case 7:ViewOverDueMatterInMenu();break; 
                    case 8:DeleteMatterInMenu(id);break;
                    case 9:CancelFinishMatterInMenu(id); break;
                    case 10:System.Environment.Exit(0); break;
                       
                }
            }
        }





        public List<string> PrintDetailedListInfro(List<DetailedList> List)
        {
            List<string> stringList = new List<string>();
            foreach (var list in List)
                stringList.Add( "\nDetailedListId:" + list.ListId +
                                "\nCreat time:    " + list.CreatTime + 
                                "\nList name:     " + list.NameOfList);

            return stringList;
        }


        public void CommonPrint()
        {
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
            Console.Clear();
        }
        public List<string> printMatter(List<Matter> List)
        {
            if(List != null)
            {
                List<string> stringList = new List<string>();
                foreach (var list in List)
                    stringList.Add("Maatter ID:" + list.MatterId + "\n" +
                                   "backlog:  " + list.Content + "\n" +
                                   "Status:   " + (list.Status == true ? "Finish" : "Unfinish") + "\n" +
                                   "CreatTime:" + list.CreatTime + "\n" +
                                   "Past time:" + list.PastTime + "\n" +
                                   "Remark:   " + list.Remark);
                return stringList;
            }
            else
            {
                List<string> stringList = new List<string>();
                stringList.Add("There are no events at the moment");
                return stringList;
            }
                
           

        }

        public static void SignUpInMenu()
        {
            OperateUser operateUser = new OperateUser();
            Console.Write("please input yuor nick name:");
            string nickName = Console.ReadLine();
            Console.Write("please input your password:");
            string passWorld = Console.ReadLine();
            operateUser.SignUp(passWorld, nickName);
            Console.WriteLine("\nSign Up Success !");
            Console.WriteLine("Please remmeber your Id number:" + _ctx.Users.Max(a => a.UserId) +
                               "\nAnd your password:" + passWorld);
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
            Console.Clear();

        }

        public static void SignInMenu()
        {
            Program system = new Program();
            OperateUser operateUser = new OperateUser();
            Console.Write("please input your ID number:");
            string inputnumber = Console.ReadLine();
            int id = Int32.Parse(inputnumber);
            Console.Write("please input your password:");
            string PassWorld = Console.ReadLine();

            if (operateUser.SignIn(id, PassWorld) == true)
            {
                Console.WriteLine("\nLogin Success !");
                Console.WriteLine("press any key to continue...");
                Console.ReadKey();
                Console.Clear();

                system.LoginSystem(id);

            }

            else
            {
                Console.WriteLine("\nLogin Failed ,Id or password wrong!");
                Console.WriteLine("press any key to continue...");
                Console.ReadKey();
                Console.Clear();
            }
        }



        public static void Main(string[] args)
        {
            
            //DataSource _data = new DataSource();
            //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 
            Console.Title = "ToDoList"; //设置控制台窗口的标题 
            while (true)
            {
             
                Console.WriteLine("                            ToDOList\n\n" +
                                  "1.Sign up                    2.Sign in                  3.Exit\n");
                Console.Write("please input you choice:");
                string inputNumber = Console.ReadLine();
                int select = Int32.Parse(inputNumber);

                switch (select)
                {
                    case 1:
                        {
                            SignUpInMenu();
                        };
                        break;
                    case 2:
                        {
                            SignInMenu();
                        };
                        break;
                    case 3: System.Environment.Exit(0); break;

                }

            }



        }
    }
}
